﻿<%@ Page Language="C#" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Linq" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script src="../../Scripts/jquery-1.4.3.min.js" type="text/javascript"></script>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        int exception = 0;
        HttpCookie _user = Request.Cookies["userinfo"];
        try
        {
            if (Request.Cookies["userinfo"] != null)
            {
                if (_user["username"] != null)
                {
                    security baomat = new security();
                    using (Up1VNDataContext dc = new Up1VNDataContext())
                    {
                        string tenDN = Session["Doanhnghiep_ID"].ToString();
                        var user_id = dc.Users.Single(u=>u.Users_Name==_user["username"]);
                        //string para = Page.RouteData.GetRequiredString("Id");
                        //int maDN = int.Parse(Page.RouteData.Values["Id"].ToString());
                        //var user_lucky = dc.USP_LuckCycle_GetByEStoreId((long)maDN, user_id.Users_Id);
                        var user_lucky = from o in dc.LuckCycles
                                         join es in dc.EStores on o.EStore_Id equals es.EStore_Id
                                         where tenDN.Replace("-"," ").ToLower().Trim() == es.EStore_Name.ToLower()
                                         select new { 
                                            o.LuckCycle_Advertizer, o.LuckCycle_AdvertizerId,o.LuckCycle_Blanks, o.LuckCycle_Points,
                                            o.LuckCycle_Coupon,o.LuckCycle_Prize,o.LuckCycle_Code,o.LuckCycle_SpinTotal,
                                            es.EStore_Id
                                         };
                        var Letters = from cl in dc.LuckCycleLetters
                                      join l in dc.LuckCycles on cl.LuckCycle_Id equals l.LuckCycle_Id
                                      join es in dc.EStores on l.EStore_Id equals es.EStore_Id
                                      //where es.EStore_Id == (long)maDN
                                      where tenDN.Replace("-", " ").ToLower().Trim() == es.EStore_Name.ToLower()
                                      select cl;
                        var solved = from ULC in dc.UsersLuckCycles
                                     join LC in dc.LuckCycles on ULC.LuckCycle_Id equals LC.LuckCycle_Id
                                     join es in dc.EStores on LC.EStore_Id equals es.EStore_Id
                                     //where LC.EStore_Id == (long)maDN && ULC.Users_Id == user_id.Users_Id
                                     where tenDN.Replace("-", " ").ToLower().Trim() == es.EStore_Name.ToLower() && ULC.Users_Id == user_id.Users_Id
                                     select new { ULC.UsersLuckCycle_Solved };
                        if (user_lucky.Count() > 0)
                        {
                            var item = user_lucky.Single();
                            Response.Write("member" + _user["username"]);
                            Response.Write("&member_id=" + user_id.Users_Id);
                            Response.Write("&advertizer=" + item.LuckCycle_Advertizer);
                            Response.Write("&advertizer_id=" + item.LuckCycle_AdvertizerId);
                            Response.Write("&can_spin=0&");
                            Response.Write("&spin_auto=false");
                            Response.Write("&spin_delay=7");
                            Response.Write("&blanks=" + item.LuckCycle_Blanks);
                            Response.Write("&points=" + item.LuckCycle_Points);
                            Response.Write("&coupon=" + item.LuckCycle_Coupon);
                            Response.Write("&prize=" + item.LuckCycle_Prize);
                            Response.Write("&code=" + item.LuckCycle_Code);
                            Response.Write("&spin_total=" + item.LuckCycle_SpinTotal);
                            Response.Write("&solved=" + solved.Single().UsersLuckCycle_Solved);
                            int row_count = Letters.Count();
                            int j = 1;
                            foreach (var item_index in Letters)
                            {
                                if (j <= row_count)
                                    Response.Write("&probability" + j + "=" + item_index.LuckCycleLetter_Letter);
                                j++;
                            }
                            for (int k = row_count + 1; k < 17; k++)
                            {
                                Response.Write("&probability" + k + "=1");
                            }
                            Response.Write("&end=1");
                        }
                    }
                }
                else
                {
                    exception = -1;
                }
            }
            else
            {
                exception = -1;
            }
        }
        catch (Exception ex)
        {
            exception = -1;
        }
        finally
        {
            if (exception == -1)
            {
                Response.Write("member=Đăng nhập để quay&");
                Response.Write("member_id=&");
                Response.Write("advertizer=up1.vn&");
                Response.Write("advertizer_id=&");
                Response.Write("can_spin=-1&");
                Response.Write("spin_auto=false&");
                Response.Write("spin_delay=7&");
                Response.Write("blanks=2&");
                Response.Write("points=&");
                Response.Write("coupon=up1.vn&");
                Response.Write("prize=up1.vn&");
                Response.Write("code=up1.vn&");
                Response.Write("spin_total=-1&");
                Response.Write("solved=&");
                Response.Write("probability1=1&");
                Response.Write("probability2=1&");
                Response.Write("probability3=1&");
                Response.Write("probability4=1&");
                Response.Write("probability5=1&");
                Response.Write("probability6=1&");
                Response.Write("probability7=1&");
                Response.Write("probability8=1&");
                Response.Write("probability9=1&");
                Response.Write("probability10=1&");
                Response.Write("probability11=1&");
                Response.Write("probability12=1&");
                Response.Write("probability13=1&");
                Response.Write("probability14=1&");
                Response.Write("probability15=1&");
                Response.Write("probability16=1&");
                Response.Write("end=1");
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('Vui lòng đăng nhập trước khi quay');</acript>");
            }
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div id="result">
    
    </div>
    </form>
</body>
</html>
